package com.desetalk.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import android.os.Handler;
import android.os.Message;

import com.desetalk.proto.ResponseProtocol.Credit;
import com.desetalk.proto.ResponseProtocol.Response;
import com.desetalk.proto.ResponseProtocol.Room;
import com.desetalk.proto.ResponseProtocol.Score;
import com.desetalk.proto.ResponseProtocol.User;

public class ClientHandler extends SimpleChannelInboundHandler<Response> {
	private Handler uiHandler; // 声明一个Handler对象
    @Override
    public void channelRead0(ChannelHandlerContext ctx, Response response) throws Exception {
    	if (response.hasLobby()) {
    		// 大厅
    		for(Room room : response.getLobby().getRoomList()){
    			printRoomInfo(room);
    		}
    	} else if (response.hasUserNum()) {
    		// 人数
    		
    		System.out.println(response.getUserNum());
    	} else if (response.hasCreateRoom()) {
    		// 创建房间返回信息
    		if(response.getCreateRoom().getSuccess()){
    			System.out.println("房间创建成功，房间号为：" + response.getCreateRoom().getRoomId());
    			
    		} else {
    			System.out.println("房间创建失败！");
    		}
    	} else if (response.hasSetCourse()) {
    		// 设置课程返回状态
    		if(response.getSetCourse()){
    			System.out.println("设置课程成功！");
    		} else {
    			System.out.println("设置课程失败！");
    		}
    	} else if (response.hasCurrentCourse()) {
    		// 当前房间课程
    		System.out.println("当前房间语言为：" + response.getCurrentCourse().getLang());
    		System.out.println("当前房间课程号为：" + response.getCurrentCourse().getCourseId());
    	} else if (response.hasEnterRoom()) {
    		// 加入房间返回信息
    		if(response.getEnterRoom().getSuccess()){
    			System.out.println("加入房间成功！");
    			printRoomInfo(response.getEnterRoom().getRoom());
    		} else {
    			System.out.println("加入房间失败！");
    		}
    	} else if (response.hasSearchRoom()) {
    		// 搜索房间返回信息
    		if(response.getSearchRoom().getSuccess()){
    			System.out.println("房间搜索成功，房间号为：" + response.getSearchRoom().getRoom());
    		} else {
    			System.out.println("房间搜索失败！");
    		}
    	} else if (response.hasEnterUser()) {
    		// 加入房间的用户
    		System.out.print("有新用户加入房间，");
    		printUserInfo(response.getEnterUser());
    	} else if (response.hasReady()) {
    		// 用户准备状态
    		if(response.getReady().getState()){
    			System.out.println("用户" + response.getReady().getId() + "已准备！");
    		} else {
    			System.out.println("用户" + response.getReady().getId() + "取消准备！");
    		}
    	} else if (response.hasQuitUserId()) {
    		// 退出房间的用户id
    		System.out.println("退出房间的用户id为：" + response.getQuitUserId());
    	} else if (response.hasOwnerId()) {
    		// 当前房主id
    		System.out.println("当前房主id为：" + response.getOwnerId());
    	} else if (response.hasStartStudy()) {
    		// 开始学习
    		if(response.getStartStudy()){
    			System.out.println("开始学习！");
    		} else {
    			System.out.println("开始学习失败！");
    		}
    	} else if (response.hasSentence()) {
    		// 英语句子
    		System.out.println("句子顺序：" + response.getSentence());
    	} else if (response.hasRepeat()) {
    		// 最高分用户跟读及得分列表
    		System.out.println("最高分用户id为：" + response.getRepeat().getHighScoreUserId());
    		System.out.println("用户跟读：" + response.getRepeat().getVoice());
    		
    		for(Score score : response.getRepeat().getScoreListList()){
    			printScoreInfo(score);
    		}
    	} else if (response.hasEndStudy()) {
    		// 学习结束
    		if(response.getEndStudy()){
    			System.out.println("学习结束！");
    		} else {
    			System.out.println("学习结束失败！");
    		}
    	} else if (response.hasRoomChat()) {
    		// 房间聊天信息
    		System.out.println("用户" + response.getRoomChat().getUserId() + "说：" + response.getRoomChat().getVoice());
    	} else if (response.hasGameChat()) {
    		// 游戏聊天信息
    		System.out.println("用户" + response.getGameChat().getUserId() + "说：" + response.getGameChat().getMessage());
    	} else if (response.hasException()) {
    		// 异常信息
    		System.out.println(response.getException());
    	} else if (response.hasInGame()) {
    		// 是否在游戏中
    		if (response.getInGame()) {
    			System.out.println("在游戏中！");
    		} else {
    			System.out.println("不在游戏中！");
    		}
    	} else if (response.hasOlState()) {
    		// 在线状态
    		
    	} else if (response.hasCreditList()) {
    		// 用户积分列表
    		for(Credit credit : response.getCreditList().getCreditList()){
    			System.out.println("用户：" + credit.getUserId() + "，当前积分：" + credit.getCurrent() + "，总积分：" + credit.getTotal());
    		}
    	} else if (response.hasRefreshRoom()) {
    		// 刷新房间信息响应
    		
    	} else if (response.hasIsKicked()) {
    		// 被踢出房间
    		System.out.println("被踢出房间！");
    	} else if (response.hasIsGirlModel()) {
    		// 房间是否为妹子模式
    		if(response.getIsGirlModel().getIsGirlModel()){
    			System.out.println("房间：" + response.getIsGirlModel().getRoomId() + " 处于妹子模式中！");
    		} else {
    			System.out.println("房间：" + response.getIsGirlModel().getRoomId() + " 取消妹子模式！");
    		}
    	}
    	Message m = uiHandler.obtainMessage(); // 获取一个Message
    	m.arg1=1;
    	m.obj=response;
    	uiHandler.sendMessage(m);
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
    
    /**
     * 打印房间信息
     * @param room
     */
    private void printRoomInfo(Room room){
    	System.out.println("房间信息：房间号：" + room.getId() + "，房主id：" + room.getOwnerId() + "，课程id：" + room.getCourseId() + 
    			"，用户个数：" + room.getUserCount() + "，房间状态：" + (room.getState() ? "学习未开始" : "学习进行中"));
    	
    	for(User user : room.getUserList()){
    		printUserInfo(user);
    	}
    	System.out.println();
    	
    }
    
    /**
     * 打印用户信息
     * @param user
     */
    private void printUserInfo(User user){
    	System.out.println("用户信息：id：" + user.getId() + "，用户名：" + user.getName() + "，头像：" + user.getFigureUrl() + "，准备状态：" + (user.getReady() ? "已准备" : "未准备"));
    }
    
    /**
     * 打印得分信息
     * @param score
     */
    private void printScoreInfo(Score score){
    	System.out.println("用户" + score.getUserId() + "得分为：" + score.getScore());
    }
	public Handler getUiHandler() {
		return uiHandler;
	}
	public void setUiHandler(Handler uiHandler) {
		this.uiHandler = uiHandler;
	}
}
